package com.zhdl.modules.prefiresys.web.controller;

import com.alibaba.fastjson2.JSON;
import com.zhdl.common.util.WebResult;
import com.zhdl.common.ytools.HttpClientUtil;
import com.zhdl.modules.prefiresys.entity.Detector;
import com.zhdl.modules.prefiresys.entity.DetectorAreaInfo;
import com.zhdl.modules.prefiresys.service.DetectorAreaInfoService;
import com.zhdl.modules.prefiresys.service.DetectorService;
import com.zhdl.modules.prefiresys.web.request.DetectorInsertRequest;
import com.zhdl.modules.prefiresys.web.request.DetectorUpdateRequest;
import com.zhdl.modules.prefiresys.web.request.GetAreaListRequest;
import com.zhdl.modules.prefiresys.web.response.AllDetectorListResp;
import com.zhdl.modules.prefiresys.web.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 探測器設備接口
 */
@RestController
@RequestMapping("/api/v1/detector")
public class DetectorController {

    @Autowired
    DetectorService detectorService;

    @Autowired
    DetectorAreaInfoService detAreaInfoService;

    @Autowired
    CollectService collectService;

    /**
     * 查询探测设备列表
     *
     * @return
     */
    @PostMapping(path = "/list")
    public WebResult<List<AllDetectorListResp>> getDetList() {
        List<AllDetectorListResp> list = detectorService.getAllDetectorlist();
        return WebResult.okResult(list);
    }

    /**
     * 查询所有防区列表，可以只查询霜一个探测器ID
     *
     * @return
     */
    @PostMapping(path = "/area/list")
    public WebResult<List<DetectorAreaInfo>> getAreaList(@RequestBody @Validated GetAreaListRequest request) {
        List<DetectorAreaInfo> list = detAreaInfoService.findListByDetIds(request.getDetectorId());
        return WebResult.okResult(list);
    }

    /**
     * 根据ID查询某一个探测设备
     *
     * @param id 探测器Ids
     * @return
     */
    @GetMapping(path = "/getById/{id}")
    public WebResult<Detector> getDetectorById(@PathVariable String id) {
        Detector comSp = detectorService.selectById(id);
        return WebResult.okResult(comSp);
    }

    /**
     * 添加探测设备
     *
     * @param request
     * @return
     */
    @PostMapping(path = "/insert")
    public WebResult<Detector> addDetector(@RequestBody @Validated DetectorInsertRequest request) {
        Detector detector = detectorService.addDetector(request);

        //更新内存信息
        collectService.BuildMemDev();

        //6-12
        //先同步detector
        String jsonString = JSON.toJSONString(detector);
        String urlPath = new String("/api/detector/addDetector");
        String returnResult1 = HttpClientUtil.reportDataByHttp(jsonString, urlPath);

        //然后再同步DetectorAreaInfo
        List<String> detectorIds = new ArrayList<>();
        detectorIds.add(detector.getId());
        List<DetectorAreaInfo> detectorAreaInfoList = detAreaInfoService.findListByDetIds(detectorIds);

        String jsonStringAreaInfo = JSON.toJSONString(detectorAreaInfoList);
        String urlPathAreaInfo = new String("/api/detector/addDetectorAreaInfo");
        String returnResult2 = HttpClientUtil.reportDataByHttp(jsonStringAreaInfo, urlPathAreaInfo);

        return WebResult.okResult(detector);
    }

    /**
     * 编辑探测设备
     *
     * @param request
     * @return
     */
    @PostMapping(path = "update")
    public WebResult<Detector> updateDetector(@RequestBody @Validated DetectorUpdateRequest request) {
        //更新探测器设备
        Detector newDto = detectorService.updateDetector(request);

        // 更新内存信息
        collectService.BuildMemDev();

        //6-12
        //System.out.println("request.getProjectId():"+request.getProjectId());//6-9
        String jsonString = JSON.toJSONString(newDto);
        String urlPath = new String("/api/detector/addDetector");
        String returnResult = HttpClientUtil.reportDataByHttp(jsonString, urlPath);

        return WebResult.okResult(newDto);
    }

    /**
     * 删除探测设备
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "delete/{id}")
    public WebResult deleteDetector(@PathVariable("id") String id) {
        //删除探测器设备
        detectorService.deleteDetector(id);

        // 更新内存信息
        collectService.BuildMemDev();

        return WebResult.okResult();
    }

}
